package com.guchenbo.example.dfs;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author guchenbo
 * @date 2024/7/30
 */
public class TreeNodeSorter {

    public List<String> getNames() {
        return new ArrayList<>();
    }

    public List<String> buildTreeAndSort(List<String> names) {
        if (CollectionUtil.isEmpty(names)) {
            return names;
        }
        Map<String, TreeNode> treeMap = new HashMap<>();
        names.stream().map(name -> {
            TreeNode treeNode = computeIfAbsent(treeMap, name);
            List<String> children = getChildren(treeNode);
            if (!CollectionUtil.isEmpty(children)) {
                treeNode.setChildren(new ArrayList<>());
                children.forEach(child -> {
                    TreeNode childTreeNode = computeIfAbsent(treeMap, child);
                    childTreeNode.setRoot(false);
                    treeNode.getChildren().add(childTreeNode);
                });
            }
            return treeNode;
        }).collect(Collectors.toList());

        List<TreeNode> treeNodes = new ArrayList<>();
        return null;
    }

    private List<String> getChildren(TreeNode treeNode) {
        return null;
    }

    private TreeNode computeIfAbsent(Map<String, TreeNode> treeMap, String name) {
        return treeMap.computeIfAbsent(name, k -> TreeNode.builder().name(k).root(true).build());
    }

}
